<script setup>
import { ref } from 'vue';
import axios from 'axios';
import { useRouter } from 'vue-router';

const router = useRouter();


const props = defineProps({
  show: Boolean,
  price: Number,
  onClose: Function,
  onSuccess: Function
});

const selectedPayType = ref(1); // Default to Alipay

const handlePayment = async () => {
  try {
    // Get consultation data from localStorage
    const textData = JSON.parse(localStorage.getItem('userInputData') || '{}');
    const doctorId = JSON.parse(localStorage.getItem('doctorId'));
    const selectedCoupon = JSON.parse(localStorage.getItem('selectedCoupon'));
    console.log('242424242424',selectedCoupon);

    // Create order
    const response = await axios.post('http://127.0.0.1:8000/orders/orders/', {
      pay_type: selectedPayType.value,
      user_coupon_id: selectedCoupon?.user_coupon_id,
      doctor_id: doctorId,
      discount_name: selectedCoupon?.name || "通用类型",
      

    },
      {
        headers: {
          'Authorization': `Bearer ${localStorage.getItem('token')}` // Assuming token auth
        } });
    console.log('11111111111111111111',response);
      
    if (response.data) {
      // Store order info
      localStorage.removeItem('selectedCoupon')
      localStorage.setItem('currentOrder', JSON.stringify(response.data));
      
      // Redirect to payment page based on pay type
      if (selectedPayType.value === 1) { // Alipay
        axios.get(`http://127.0.0.1:8000/pay/alipay/link/?order_id=${response.data.id}`).then((res) => { 
          //  跳转到新的页面
          window.open(res.data, '_blank')
          console.log(res.data)

        });

      }        
 else { // WeChat
        // Handle WeChat payment
        console.log('WeChat payment not implemented');
      }
    }
  } catch (error) {
    console.error('Payment error:', error);
  }
};





</script>

<template>
  <div v-if="show" class="modal-overlay">
    <div class="modal-content">
      <h2>选择支付方式</h2>
      <div class="payment-options">
        <div class="payment-option" :class="{ active: selectedPayType === 1 }" @click="selectedPayType = 1">
          <img src="https://tse4.mm.bing.net/th?id=OIP.lxDbWrNtZ7lMSR_p-xIjqwHaHa&pid=Api" style="object-fit: cover; transform: scale(1.2);" alt="支付宝" />
          <span>支付宝支付</span>
        </div>
        <div class="payment-option" :class="{ active: selectedPayType === 0 }" @click="selectedPayType = 0">
          <img src="https://tse4.mm.bing.net/th?id=OIP.KX_05K6jrKo-yU66jG3mNwHaFo&pid=Api" style="object-fit: cover;" alt="微信" />
          <span>微信支付</span>
        </div>
      </div>
      <div class="price-display">
        支付金额：<span class="amount">¥{{ price }}</span>
      </div>
      <div class="modal-actions">
        <button class="cancel-btn" @click="onClose">取消</button>
        <button class="confirm-btn" @click="handlePayment">确认支付</button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  padding: 24px;
  border-radius: 12px;
  width: 90%;
  max-width: 400px;
}

.payment-options {
  display: flex;
  gap: 16px;
  margin: 24px 0;
}

.payment-option {
  flex: 1;
  padding: 16px;
  border: 2px solid #eee;
  border-radius: 8px;
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  transition: all 0.3s;
}

.payment-option.active {
  border-color: #00b578;
  background-color: #f0fff9;
}

.payment-option img {
  width: 32px;
  height: 32px;
}

.price-display {
  text-align: center;
  margin: 24px 0;
  font-size: 18px;
}

.amount {
  color: #00b578;
  font-weight: bold;
  font-size: 24px;
}

.modal-actions {
  display: flex;
  gap: 16px;
  justify-content: flex-end;
}

.cancel-btn, .confirm-btn {
  padding: 8px 24px;
  border-radius: 20px;
  border: none;
  cursor: pointer;
}

.cancel-btn {
  background-color: #f5f5f5;
  color: #666;
}

.confirm-btn {
  background-color: #00b578;
  color: white;
}
</style>